<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        let xhr = new XMLHttpRequest()
        xhr.open("get", "http://localhost:4399/getList?name=孙悟空")
        xhr.send()
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4){
            let res = JSON.parse(xhr.responseText)
            console.log(res);
        }
    }


    // =====================
    // post新增
    function add(){
         let xhr = new XMLHttpRequest()
        xhr.open("post", "http://localhost:4399/add")
        xhr.setRequestHeader("Content-Type","application/json")
        // 默认form类型的
        //  xhr.setRequestHeader("Content-Type",
        // "application/x-www-form-urlencoded")
        // xhr.send(`name=白骨精&age=300&sex=女&address=白骨洞`)

        // json
        let obj={
            name:"红孩儿",
            age:18,
            sex:"男",
            address:"12313"
        }
        xhr.send(JSON.stringify(obj))
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4){
            let res = JSON.parse(xhr.responseText)
            console.log(res);
        }
    }
    }
    add()
    </script>
</body>
</html>